package com.hq.fxh.No3_TuiGuang.Adaoter;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;


import com.hq.fxh.Modle.ShareArticlesRes;
import com.hq.fxh.No0_ChengQu.Adapter.adapter.BaseRecAdapter;
import com.hq.fxh.No0_ChengQu.Adapter.adapter.ItemSlideHelper;
import com.hq.fxh.No0_ChengQu.Adapter.viewholder.BaseViewHolder;
import com.hq.fxh.No1_Infromation.Infor.Activity.NewsDetailsActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author 大圣
 * @ClassName StatisticAdapter
 * @Description :
 * @Created 16/6/22
 * @Email qwqe601660957@163.com
 */
public class StatisticAdapter extends BaseRecAdapter<ShareArticlesRes.DataBean> implements ItemSlideHelper.Callback {

    private RecyclerView mRecyclerView;
    private Context context;
    private int uid;
    private ProgressDialog progressDialog;

    public StatisticAdapter(Context context1) {
        super(context1);
        context = context1;
        mLayoutResId = R.layout.item_statistic_new;
        uid = SharedPreferenceUtils.getMemberId(context);
    }


    @Override
    protected void convert(final BaseViewHolder helper, final ShareArticlesRes.DataBean item) {
        int position = mData.indexOf(item);
        CharSequence styledText = Html.fromHtml(item.getTitle());
        helper.setText(R.id.tvTitle, styledText);
        //日期
//        String time = RelativeDateFormat.format(getTimetoDate(item.getPtime()));
//        helper.setText(R.id.tvTime, time);
        String type = null;
        if (item.getArticle_type() == 1) {//[type:1视频 2美女 3搞笑 4新闻]
            helper.setInvisible(R.id.tvType, true);
            type = "视频";
        } else if (item.getArticle_type() == 2) {
            helper.setInvisible(R.id.tvType, true);
            type = "美图";
        } else if (item.getArticle_type() == 3) {
            helper.setInvisible(R.id.tvType, true);
            type = "搞笑";
        } else if (item.getArticle_type() == 4) {
//            type = "新闻";
            helper.setInvisible(R.id.tvType, false);
        }
        helper.setText(R.id.tvType, type);
//2016-11-24 15:59:36 获取文章分享时年月日
        String date = item.getPtime().trim();
        String date2 = date.split(" ")[0];
        String[] dateSplit = date2.split("-");
        String year = dateSplit[0];
        String month = dateSplit[1];
        String day = dateSplit[2];
        if (position > 0) {
            //获取下一条数据分享日期
            String nextDate = mData.get(position - 1).getPtime().trim().split(" ")[0];
            if (nextDate.equals(date2)) {//同一天分享的数据
                helper.setVisible(R.id.rl_data, false);
            } else {
                helper.setVisible(R.id.rl_data, true);
            }
        } else {
            helper.setVisible(R.id.rl_data, true);
        }
        //获取当前年月日
        Calendar calender = Calendar.getInstance();
        int curYear = calender.get(Calendar.YEAR);
        int curMonth = calender.get(Calendar.MONTH) + 1;//获取到的月份是0-11
        int curDay = calender.get(Calendar.DATE);
        if (curYear == Integer.parseInt(year) && curMonth == Integer.parseInt(month) && curDay == Integer.parseInt(day)) {
            helper.setTextColor(R.id.tv_month, context.getResources().getColor(R.color.new_red));
            helper.setTextColor(R.id.tv_day, context.getResources().getColor(R.color.new_red));
        } else {
            helper.setTextColor(R.id.tv_month, context.getResources().getColor(R.color.textgrey));
            helper.setTextColor(R.id.tv_day, context.getResources().getColor(R.color.textgrey));
        }
        helper.setText(R.id.tv_month, month + "月");
        helper.setText(R.id.tv_day, day);
        helper.setText(R.id.tvExposeNum, "曝光 " + item.getLook_num());
        helper.setText(R.id.tvClickNum, "点击 " + item.getClick_num());
        helper.setText(R.id.tvForwardNum, "转发 " + item.getTrans_num());
        helper.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog = ProgressDialog.show(context, "", "正在删除，请稍后……");
                progressDialog.setCancelable(true);//设置进度条是否可以按退回键取消
                progressDialog.setCanceledOnTouchOutside(true);
                deleteArticle(uid, item.getDocid(), helper.getAdapterPosition());
            }
        });
        helper.setOnClickListener(R.id.rl_staInfo, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle pBundle = new Bundle();
                String docID = item.getDocid();
                String title = item.getTitle();
                String time = item.getPtime();
                String url = item.getShare_url();
                String titlePicUrl = item.getTitle_pic_url();
                pBundle.putInt("article_id", item.getArticle_id());
                pBundle.putString("docID", docID);
                pBundle.putString("title", title);
                pBundle.putString("summary", item.getSummary());
                pBundle.putString("time", time);
                pBundle.putString("shareUrl", url);
                pBundle.putString("titlePicUrl", titlePicUrl);

                /**
                 * 跳转到统计详情界面
                 */
                LogUtils.e("跳转到统计详情界面");
                Intent intent = new Intent(context, NewsDetailsActivity.class);
                intent.putExtra("url", url);
                context.startActivity(intent);

                //  IntentUtils.openActivity(mContext, StatisticsDetailsActivity.class, pBundle);
            }
        });
    }

    //字符串转时间戳
    public static String getTime(String timeString) {
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date d;
        try {
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }

    public static Date getTimetoDate(String timeString) {
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date d = null;
        try {
            d = sdf.parse(timeString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        mRecyclerView = recyclerView;
        mRecyclerView.addOnItemTouchListener(new ItemSlideHelper(mRecyclerView.getContext(), this));
    }

    @Override
    public int getHorizontalRange(RecyclerView.ViewHolder holder) {

        if (holder.itemView instanceof LinearLayout) {
            ViewGroup viewGroup = (ViewGroup) holder.itemView;
            if (viewGroup.getChildCount() == 2) {
                return viewGroup.getChildAt(1).getLayoutParams().width;
            }
        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder getChildViewHolder(View childView) {
        return mRecyclerView.getChildViewHolder(childView);
    }

    @Override
    public View findTargetView(float x, float y) {
        return mRecyclerView.findChildViewUnder(x, y);
    }

    /**
     * 删除商户文章
     *
     * @param
     * @param adapterPosition
     */
    public void deleteArticle(int uid, String docid, final int adapterPosition) {
        LogUtils.startToash(context, "暂不支持该工能");
    /*   ArticleServiceImpl.delShareArticle(uid + "", docid, new NetworkSubscriber<CommonRes>() {
            @Override
            public int onSuccess(CommonRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    ToastUtils.show(context, "删除成功");
                    remove(adapterPosition);
                    notifyDataSetChanged();
                    progressDialog.dismiss();
                } else {
                    progressDialog.dismiss();
                    ToastUtils.show(context, data.getMsg());
                }
                return 0;
            }
        }); */

    }
}
